/**
 * 
 */
package org.ejc.api.price;

import java.util.Collection;

import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.naming.NamingException;

import org.apache.log4j.Category;
import org.ejc.api.registration.RegistrationAPIBean;
import org.ejc.exception.EJCPersistenceException;
import org.ejc.persist.model.Price;
import org.ejc.persist.model.PriceGroup;
import org.ejc.persist.model.PriceType;
import org.ejc.util.EJCUtils;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;

/**
 * @author Brian Boyle
 * @ejb.bean type="Stateless" name="PriceTypeAPI"
 *           description="PriceTypeAPI session bean"
 *           local-jndi-name="org.ejc/PriceTypeAPILocalHome"
 *           jndi-name="org.ejc/PriceTypeAPIHome" view-type="both"
 * 
 * @ejb.ejb-ref ejb-name="PriceTypeAPI" ref-name="ejb/PriceTypeAPI"
 *              view-type="both"
 * 
 * @ejb.transaction type="RequiresNew"
 * @ejb.util generate = "physical"
 *
 */
public abstract class PriceTypeAPIBean implements SessionBean {

	Category log = Category.getInstance(PriceTypeAPIBean.class);
	
	/**
	 * Get a list of all the price types.
	 * @return
	 * @throws NamingException
	 * @throws EJBException
	 * @ejb.interface-method view-type="both"
	 */
	public Collection getPriceTypes(String priceGroupCd) throws NamingException, EJBException {
		Session hsession = null;
		try {
			hsession = EJCUtils.getHibernateSession();
			//Look up the id of the priceGroup using the group description
			Criteria groupCrit = hsession.createCriteria(PriceGroup.class);
			PriceGroup priceGroup = (PriceGroup)groupCrit.add(Expression.eq("desc", priceGroupCd)).uniqueResult();
			
			//Now get the list of prices based on the groupId
			Criteria crit = hsession.createCriteria(PriceType.class);
			crit.add(Expression.eq("priceGroup.id", priceGroup.getId()));
			return crit.list();
		} catch (EJCPersistenceException epe) {
			epe.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (hsession != null) {
				hsession.close();
			}
		}
		return null;
	}
	
	/**
	 * Get a Price usign the priceTypeId.
	 * @return The price object
	 * @throws NamingException
	 * @throws EJBException
	 * @ejb.interface-method view-type="both"
	 */
	public Price getPriceByPriceTypeId(Integer priceTypeId) throws NamingException, EJBException {
		Session hsession = null;
		try {
			hsession = EJCUtils.getHibernateSession();
			Price price = (Price) hsession.createCriteria(Price.class)
			.add( Expression.eq("priceType.priceTypeId", priceTypeId) )
			.uniqueResult();
			log.debug("Got price");
			return price;
		} catch (EJCPersistenceException epe) {
			epe.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (hsession != null) {
				hsession.close();
			}
		}
		return null;
	}
	
	/**
	 * Get a list of all the price types.
	 * @return
	 * @throws NamingException
	 * @throws EJBException
	 * @ejb.interface-method view-type="both"
	 */
	public Double getPriceByPriceId(Integer priceId)throws NamingException, EJBException {
		Session hsession = null;
		try {
			hsession = EJCUtils.getHibernateSession();
			Price p=(Price)hsession.load(Price.class,priceId);
			return p.getPriceAmount();
		}catch (EJCPersistenceException epe) {
			epe.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (hsession != null) {
				hsession.close();
			}
		}
		return null;
	}
	
}
